package com.example.administrator.familfinalcialmanagement;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.bean.CommonUser;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.JaveTeacher;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityAddReasionBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.CommonUserDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.JaveTeacherDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;

/**
 * Created by 86178 on 2020/1/2.
 * author mogen
 * 用心，成就一切。
 */
public class AddReasionActivity extends BaseActivity {

    ActivityAddReasionBinding binding;
    String addType;

    CommonUserDao commonUserDao = GreenDaoUtils.getSingleTon().getmDaoSession().getCommonUserDao();
    JaveTeacherDao teacherDao = GreenDaoUtils.getSingleTon().getmDaoSession().getJaveTeacherDao();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        binding = DataBindingUtil.setContentView(this,R.layout.activity_add_reasion );
        binding.setActivity( this );
        initView();
    }

    private void initView() {
        addType = getIntent().getStringExtra( "module_add_type" );
        switch (addType){
            case "teacher":
                binding.llModuleTeacherClassName.setVisibility( View.VISIBLE );
                binding.llModuleClassOneName.setVisibility( View.GONE );
                binding.llModuleClassOneNum.setVisibility( View.GONE );
                binding.llModuleClassNameTwo.setVisibility( View.GONE );
                binding.llModuleClassTwoNum.setVisibility( View.GONE );
                binding.llModuleClassNameThree.setVisibility( View.GONE );
                binding.llModuleClassThreeNum.setVisibility( View.GONE );
                break;
            case "student":
                binding.llModuleTeacherClassName.setVisibility( View.GONE );
                binding.llModuleClassOneName.setVisibility( View.VISIBLE );
                binding.llModuleClassOneNum.setVisibility( View.VISIBLE );
                binding.llModuleClassNameTwo.setVisibility( View.VISIBLE );
                binding.llModuleClassTwoNum.setVisibility( View.VISIBLE );
                binding.llModuleClassNameThree.setVisibility( View.VISIBLE );
                binding.llModuleClassThreeNum.setVisibility( View.VISIBLE );
                break;
                default:
        }

        binding.tvModuleUp.setOnClickListener( this );
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tvModuleUp:
                switch (addType){
                    case "teacher":
                        String teacherName = binding.tvModuleName.getText().toString().trim();
                        if (TextUtils.isEmpty(teacherName)) {
                            ToastUtil.showToast("请输入名字");
                            return;
                        }

                        String teacherPhone = binding.tvModulePhone.getText().toString().trim();
                        if (TextUtils.isEmpty(teacherPhone)) {
                            ToastUtil.showToast("请输入账号");
                            return;
                        }

                        String teacherPwd = binding.etModulePwd.getText().toString().trim();
                        if (TextUtils.isEmpty( teacherPwd )) {
                            ToastUtil.showToast( "请输入密码" );
                            return;
                        }

                        String teacherClassName = binding.etModuleClassName.getText().toString().trim();
                        if (TextUtils.isEmpty( teacherClassName )) {
                            ToastUtil.showToast( "请输入负责课程" );
                            return;
                        }

                        JaveTeacher teacher = new JaveTeacher(  );
                        teacher.setName( teacherName );
                        teacher.setPhone( teacherPhone );
                        teacher.setPwd( teacherPwd );
                        teacher.setClassName( teacherClassName );
                        teacher.setOne( "2" );
                        teacherDao.insert( teacher );
                        finish();
                        break;
                    case "student":
                        String name = binding.tvModuleName.getText().toString().trim();
                        if (TextUtils.isEmpty(name)) {
                            ToastUtil.showToast("请输入名字");
                            return;
                        }

                        String phone = binding.tvModulePhone.getText().toString().trim();
                        if (TextUtils.isEmpty(phone)) {
                            ToastUtil.showToast("请输入账号");
                            return;
                        }

                        String pwd = binding.etModulePwd.getText().toString().trim();
                        if (TextUtils.isEmpty( pwd )) {
                            ToastUtil.showToast( "请输入密码" );
                            return;
                        }

                        String classNameOne = binding.tvModuleClassOneName.getText().toString().trim();
                        String classNameTwo = binding.tvModuleClassNameTwo.getText().toString().trim();
                        String classNameThree = binding.tvModuleClassNameThree.getText().toString().trim();

                        String classOneNum = binding.etModuleClassOneNum.getText().toString().trim();
                        if (TextUtils.isEmpty(classOneNum)) {
                            ToastUtil.showToast("请输入科目一分数");
                            return;
                        }

                        String classTwoNum = binding.etModuleClassTwoNum.getText().toString().trim();
                        if (TextUtils.isEmpty(classTwoNum)) {
                            ToastUtil.showToast("请输入科目二分数");
                            return;
                        }

                        String classThreeNum = binding.etModuleClassThreeNum.getText().toString().trim();
                        if (TextUtils.isEmpty(classThreeNum)) {
                            ToastUtil.showToast("请输入科目三分数");
                            return;
                        }

                        CommonUser commonUser = new CommonUser(  );
                        commonUser.setName(name);
                        commonUser.setPwd(pwd);
                        commonUser.setPhone(phone);
                        commonUser.setClassNameOne(classNameOne);
                        commonUser.setClassOneNum(classOneNum);
                        commonUser.setClassNameTwo(classNameTwo);
                        commonUser.setClassTwoNum(classTwoNum);
                        commonUser.setClassNameThree(classNameThree);
                        commonUser.setClassThreeNum(classThreeNum);
                        commonUser.setPwdStatus( "2" );
                        commonUserDao.insert(commonUser);
                        ToastUtil.showToast("添加完毕");
                        finish();
                        break;
                        default:
                }
                break;
                default:
        }
    }
}
